//
// Created by ct on 2021/2/2.
//

#include <iostream>
#include <cstring>

#include "sm4.h"


int test_sm4() {

//    static const char k0[16] = {
//            0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
//            0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
//    };


    static const uint8_t k[sm4::SM4_BLOCK_SIZE] = {
            0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
            0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
    };

//    static const uint8_t input[sm4::SM4_BLOCK_SIZE] = {
//            0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x77, 0x6F,
//            0x72, 0x6C, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00,
//    };


    static const uint8_t input[sm4::SM4_BLOCK_SIZE] = {
            0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
            0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
    };


    /*
    * This test vector comes from Example 1 of GB/T 32907-2016,
    * and described in Internet Draft draft-ribose-cfrg-sm4-02.
    */
    static const uint8_t expected[sm4::SM4_BLOCK_SIZE] = {
            0x68, 0x1e, 0xdf, 0x34, 0xd2, 0x06, 0x96, 0x5e,
            0x86, 0xb3, 0xe9, 0x4f, 0x53, 0x6e, 0x42, 0x46
    };

    /*
    * This test vector comes from Example 2 from GB/T 32907-2016,
    * and described in Internet Draft draft-ribose-cfrg-sm4-02.
    * After 1,000,000 iterations.
    */
//    static const uint8_t expected_iter[sm4::SM4_BLOCK_SIZE] = {
//            0x59, 0x52, 0x98, 0xc7, 0xc6, 0xfd, 0x27, 0x1f,
//            0x04, 0x02, 0xf8, 0x04, 0xc3, 0x3d, 0x3f, 0x66
//    };

    int i;
    sm4::SM4_KEY key;
    uint8_t block[sm4::SM4_BLOCK_SIZE];

    sm4::SM4_set_key(k, &key);

    memcpy(block, input, sm4::SM4_BLOCK_SIZE);

//    for(auto v : key.rk){
//        std::cout<<  unsigned(v) << " ";
//    }
//    std::cout<<std::endl;
    std::cout<<"密钥: ";
    for (auto v : k) {
        std::cout<<std::hex << unsigned(v) << " ";
    }
    std::cout << std::endl;
    std::cout<<"原文: ";
    for (auto v : input) {
        std::cout<< std::hex << unsigned(v) << " ";
    }
    std::cout << std::endl;
    sm4::SM4_encrypt(block, block, &key);
    std::cout<<"密文: ";
    for (auto v : block) {
        std::cout << std::hex << unsigned(v) << " ";
    }
    std::cout << std::endl;
    sm4::SM4_decrypt(block, block, &key);
    std::cout<<"解密: ";
    for (auto v : block) {
        std::cout<< std::hex << unsigned(v) << " ";
    }
    return 0;
}


int main() {
    test_sm4();
}
